package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.tbs.Md5Tool;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.util.TCRequestUtil;
import com.utilcode.util.ToastUtils;

import java.util.HashMap;


/**
 * 修改密码
 * Created by yqs on 2018/06/05
 * @author yqs
 */

public class ChangePasswordActivity extends TCBaseActivity implements View.OnClickListener {
    public static final String TAG = "ChangePasswordActivity";


    //private String number;

   // private boolean needLogOut = false;

    private EditText etOldpwd;
    private EditText etPwd;
    private EditText etEnterPwd;
    private Button buttonResetPwd;


    /**
     * @param context
     */
    public static void start(Context context) {
        Intent starter = new Intent(context, ChangePasswordActivity.class);
//        starter.putExtra("number", number);
//        starter.putExtra("needLogOut", needLogOut);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_change_password);
        initView();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }


    private void initView() {
        etOldpwd = (EditText) findViewById(R.id.et_oldpwd);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        etEnterPwd = (EditText) findViewById(R.id.et_enter_pwd);
        buttonResetPwd = (Button) findViewById(R.id.button_reset_pwd);
        buttonResetPwd.setOnClickListener(this);

    }

    private void initData() {
       // number = getIntent().getStringExtra("number");
        //needLogOut = getIntent().getBooleanExtra("needLogOut", false);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.common_text_change_pw));
        setEnableScreenShotListen(false);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_reset_pwd) {
            changePwdToServer();
        }
    }

    private void changePwdToServer() {
        String oldPwd = etOldpwd.getText().toString();
        String pwd = etPwd.getText().toString().trim();
        String enterPwd = etEnterPwd.getText().toString().trim();
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtils.showShort(R.string.common_text_raw_pw_not_null);
            return;
        }

        if (!Md5Tool.hashKey(oldPwd).equals(EnvironmentVariable.getPassword())) {
            ToastUtils.showShort(R.string.common_text_raw_pw_wrong);
            return;
        }
        if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd)) {
            ToastUtils.showShort(R.string.common_text_pw_not_null);
            return;
        }
        if (pwd.length() < 8) {
            ToastUtils.showShort(R.string.common_text_pw_length);
            return;
        }
        if (TextUtils.equals(oldPwd, enterPwd)) {
            ToastUtils.showShort(R.string.common_text_pw_raw_now_pw_same);
            return;
        }
        if (!TextUtils.equals(pwd, enterPwd)) {
            ToastUtils.showShort(R.string.common_text_pw_repeat_input_not_same);
            return;
        }

        change();

    }


    public void change() {
        showLoading(R.string.common_text_pw_changing);
        String accessToken = EnvironmentVariable.getProperty("tc_access_token", "");
        //使用accesstoken重置密码
        HashMap<String, String> params = new HashMap<>();
        params.put("newPassword", etEnterPwd.getText().toString());
        params.put("access_token", accessToken);
        TCRequestUtil.getCommonRequest(TAG, "user/resetPassword", params, new TCRequestUtil.TCRequestCallback() {
            @Override
            public void onSuccess(String response) {
                showLoading(ResStringUtil.getString(R.string.open_planet_encrypting_wallet));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoading();
                        EnvironmentVariable.setPassword(Md5Tool.hashKey(etEnterPwd.getText().toString()));
                        ToastUtils.showShort(R.string.common_text_pw_change_pw_success);
                        finish();
                    }
                },3600);



            }

            @Override
            public void onFail(String error) {
                dismissLoading();
                ToastUtils.showShort(R.string.common_text_pw_change_pw_fail);
            }
        });
    }


}